//
//  HiddenChatTableView.m
//  BaiYing_Thinker
//
//  Created by 鹏 朱 on 15/11/19.
//  Copyright (c) 2015年 XKJH. All rights reserved.
//

#import "HiddenChatTableView.h"
#import "SessonContentMessage.h"
#import "TouchTableView.h"
#import "AppDelegate.h"
#import "DatabaseCache.h"
#import "SubjectMessage.h"
#import "SessionMessage.h"
#import "XKO_CreateUIViewHelper.h"
#import "NSString+Teshuzifu.h"
#import "FileMangeHelper.h"
#import "UploadFile.h"
#import "PlayerHelper.h"
#import "VoiceView.h"
#import "TouchTableView.h"
#import "ChatViewCell.h"
#import "ChatViewCellFrame.h"
#import "XKO_CreateUIViewHelper.h"

@interface HiddenChatTableView()

@property(nonatomic, strong) NSMutableDictionary *contentIdList;
@property(nonatomic, strong) UIView *headerView;
@property(nonatomic, strong) UIView *footerView;
@property(nonatomic, strong) SessonContentMessage *subjectMessage;
@property(nonatomic, strong) SessonContentMessage *firstMessage;
@property(nonatomic, strong) UILabel *headText;

@end

@implementation HiddenChatTableView

- (id)initWithFrame:(CGRect)frame dataSource:(NSMutableArray *)dataSource {

    if (self = [super initWithFrame:frame]) {

        [self addSubview:self.tableView];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        self.dataListOne = dataSource;
        self.tableView.backgroundColor = kGetColor(18, 38, 74);
        self.contentIdList = [[NSMutableDictionary alloc] init];
        
        [self reloadData];

    }
    
    return self;
    
}

- (void)reloadData {
    
    if (!self.dataListOne || self.dataListOne.count == 0) {
        
        //开启新的话题
        NSDictionary *dic = @{@"clientid":@"BAIYING_WORKER01_CLIENT",@"content":@{@"sys_code":@"9999",@"text":@"请问有什么可以帮您的？",@"type":@"TEXT"},@"model":@"SYSTEM",@"msg_id":kAppDelegate.messageId};
        SessonContentMessage *message = [[SessonContentMessage alloc] initWithDictionary:dic isMyself:@1];
        self.subjectMessage = message;
        self.tableView.tableFooterView = [self footerView];
        self.firstMessage = nil;
        
    } else if (self.dataListOne.count == 1) {
        
        SessonContentMessage *subject = [self.dataListOne objectAtIndex:0];
        if ([subject.sys_code isEqualToString:@"9999"] || [subject.sys_code isEqualToString:@"1005"]) {
            
            self.subjectMessage = subject;
            [self.dataListOne removeObject:subject];
            self.tableView.tableFooterView = [self footerView];
            self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
            
        } else {
            
            self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        }
        
        self.firstMessage = subject;
        
    } else {
        
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        SessonContentMessage *subject = [self.dataListOne objectAtIndex:0];
        self.firstMessage = subject;
        
    }
    
    if (self.firstMessage && !IsEmpty(self.firstMessage.subject_content)) {
        
        self.tableView.tableHeaderView = [self headerView];
        _headText.text = self.firstMessage.subject_content;
        
    } else {
        
        self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
        
    }
    
    [self.tableView reloadData];
}

- (TouchTableView *)tableView {
    
    if (!_tableView) {
        TouchTableView *tableView = [[TouchTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 70) style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        //        tableView.touchDelegate = self;
        tableView.delaysContentTouches = NO;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [tableView setSeparatorInset:UIEdgeInsetsZero];
        tableView.contentOffset = CGPointMake(0, 0);
        tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectZero];
        _tableView = tableView;
    }
    
    return _tableView;
}

- (UIView *)footerView {
    
    if (!_footerView) {
        
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 120)];
        header.backgroundColor = kGetColor(18, 38, 74);
        
        UILabel *headText = [XKO_CreateUIViewHelper createLabelWithFont:[UIFont boldSystemFontOfSize:33] fontColor:[UIColor whiteColor] text:self.subjectMessage.msg_content];
        headText.numberOfLines = 0;
        headText.text = self.subjectMessage.msg_content;
        headText.textAlignment = NSTextAlignmentCenter;
        [header addSubview:headText];
        
        [headText mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(header.mas_centerX);
            make.centerY.equalTo(header.mas_centerY);
            make.width.equalTo(@(280));
        }];
        _footerView = header;
    }
    
    return _footerView;
}

- (UIView *)headerView {
    
    if (!_headerView) {
        
        UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
        header.backgroundColor = kGetColor(18, 38, 74);
        
        UILabel *headText = [XKO_CreateUIViewHelper createLabelWithFont:[UIFont boldSystemFontOfSize:18] fontColor:[UIColor whiteColor] text:self.subjectMessage.msg_content];
        headText.numberOfLines = 1;
        headText.text = self.firstMessage.subject_content;
        headText.textAlignment = NSTextAlignmentLeft;
        _headText = headText;
        [header addSubview:headText];
        
        [headText mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(header.mas_centerX);
            make.centerY.equalTo(header.mas_centerY);
            make.width.equalTo(@(280));
        }];
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10, 49, kScreenWidth-20 , 1)];
        lineView.backgroundColor = [UIColor whiteColor];
        [header addSubview:lineView];
        
        _headerView = header;
    }
    
    return _headerView;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataListOne count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SessonContentMessage *subject = [self.dataListOne objectAtIndex:indexPath.row];
    DataModelType type = [subject.msg_type isEqualToString:@"TEXT"]?TextDataModelType:[subject.msg_type isEqualToString:@"COMPONENT"]?WebDataModelType:[subject.msg_type isEqualToString:@"IMAGE"]?ImageDataModelType:[subject.msg_type isEqualToString:@"AUDIO"]?AudioDataModelType:[subject.msg_type isEqualToString:@"VIDEO"]?VideoDataModelType:TextAndImageDataModelType;
    
    CGSize webSize = CGSizeMake(kScreenWidth - 40, 0);
    
    if (type == ImageDataModelType) {
        webSize.width = [subject.content_width floatValue];
        webSize.height = [subject.content_height floatValue];
    } 
    
    if ((type == WebDataModelType || type == ImageDataModelType) && self.contentIdList[subject.session_content_id]) {
        
        webSize = CGSizeFromString(self.contentIdList[subject.session_content_id]);
    }
    
    static NSString *cellId;
    if (type == TextDataModelType) {
        cellId = @"TextDataModelTypeIdentifier";
    } else if (type == WebDataModelType) {
        cellId = @"WebDataModelTypeIdentifier";
    } else if (type == ImageDataModelType) {
        cellId = @"ImageDataModelTypeIdentifier";
    } else if (type == VideoDataModelType) {
        cellId = @"VideoDataModelTypeIdentifier";
    } else if (type == AudioDataModelType) {
        cellId = @"AudioDataModelTypeIdentifier";
    } else if (type == TextAndImageDataModelType) {
        cellId = @"TextAndImageDataModelTypeIdentifier";
    } else {
        cellId = @"TextDataModelTypeIdentifier";
    }
    
    ChatViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (!cell) {
        cell = [[ChatViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId dataModelType:type webSize:webSize];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    } else {
        cell.dataModelType = type;
        cell.webSize = webSize;
    }
    
    WEAK_SELF
    cell.webFinishLoadBlock = ^(NSString *sizeString, NSNumber *contentId){
        
        if (contentId) {
            [weak_self.contentIdList setObject:sizeString forKey:contentId];
            
            //        [weak_self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            [weak_self.tableView reloadData];
        }
    };
    
    cell.message = subject;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SessonContentMessage *subject = [self.dataListOne objectAtIndex:indexPath.row];
    DataModelType type = [subject.msg_type isEqualToString:@"TEXT"]?TextDataModelType:[subject.msg_type isEqualToString:@"COMPONENT"]?WebDataModelType:[subject.msg_type isEqualToString:@"IMAGE"]?ImageDataModelType:[subject.msg_type isEqualToString:@"AUDIO"]?AudioDataModelType:[subject.msg_type isEqualToString:@"VIDEO"]?VideoDataModelType:TextAndImageDataModelType;
    
    CGSize webSize = CGSizeMake(kScreenWidth - 40, 0);
    
    if (type == ImageDataModelType) {
        webSize.width = [subject.content_width floatValue];
        webSize.height = [subject.content_height floatValue];
    }
    if ((type == WebDataModelType || type == ImageDataModelType) && self.contentIdList[subject.session_content_id]) {
        webSize = CGSizeFromString(self.contentIdList[subject.session_content_id]);
    }
    
    ChatViewCellFrame *frame = [[ChatViewCellFrame alloc] initWithDataModel:subject dataModelType:type webSize:webSize];
    return frame.cellHeight;
}



@end
